<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Api\MiniProgramController;
use App\Http\Controllers\Controller;
use App\Models\Message;
use App\Models\Order;
use App\Models\OrderComment;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Response;
use Yansongda\LaravelPay\Facades\Pay;

class OrderCommentController extends Controller
{
    function index() {
        return view('admin.comment.index');
    }

    /**
     * 订单评论数据接口
     * @param Request $request
     * @return JsonResponse
     */
    public function data(Request $request): JsonResponse
    {
        $model = OrderComment::query();
        if ($status = $request->get('status')){
            $model->where('order_comments.status','=',$status);
        }

        $res = $model->leftJoin('order_products','order_comments.order_id','=','order_products.order_id')
            ->leftJoin('products','order_products.product_id','=','products.id')
            ->leftJoin('orders','order_comments.order_id','=','orders.id')
            ->distinct()
            ->select('order_comments.status','orders.delivery','order_comments.id','content','products.title','orders.cash_money','orders.status as order_status')
            ->orderBy('order_comments.id','desc')->paginate($request->get('limit',30));

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res->total(),
            'data'  => $res->items(),
        ];
        return Response::json($data);
    }

    //审核评论
    function audit(Request $request) {
        $request->validate([
            'id'         =>  'required',
            'status'  =>  'required'
        ]);
        $result = OrderComment::where(['id' =>$request->post('id'), 'status' => '1'])
                ->update([
                    'status'    => $request->post('status')
                ]);
        if ($result) {
            $data = [
                'code' => 0,
                'msg'   => '审核成功',
            ];
        }else {
            $data = [
                'code' => 1,
                'msg'   => '审核失败',
            ];
        }
        return Response::json($data);
    }



}
